package com.zkthink.ceres.app.store.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.cache.repository.CacheRepository;
import com.zkthink.cache.repository.RedisRepositoryImpl;
import com.zkthink.ceres.applet.pcVehicles.service.PcVehiclesService;
import com.zkthink.ceres.applet.server.service.AppletStoreServerService;
import com.zkthink.ceres.common.annotations.LoginMode;
import com.zkthink.ceres.common.constant.CacheKey;
import com.zkthink.ceres.sms.enumeration.TemplateCodeType;
import com.zkthink.ceres.store.dto.*;
import com.zkthink.ceres.store.entity.AppletWithdraw;
import com.zkthink.ceres.store.entity.StoreJoin;
import com.zkthink.ceres.store.entity.StoreWithdrawHis;
import com.zkthink.ceres.store.service.StoreJoinService;
import com.zkthink.ceres.store.service.StoreService;
import com.zkthink.ceres.store.service.StoreWithdrawHisService;
import com.zkthink.context.BaseContextHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by kellen on 2020/5/16.
 */

@Slf4j
@Validated
@RestController
@RequestMapping("/store")
@AllArgsConstructor
@LoginMode(strict = false)
@Api(value = "店铺模块", tags = "店铺模块")

public class AppStoreController {
    private StoreService storeService;
    private AppletStoreServerService appletStoreServerService;
    private RedisRepositoryImpl redisRepository;
    private StoreJoinService storeJoinService;
    @Autowired
    private CacheRepository cacheRepository;
    private StoreWithdrawHisService storeWithdrawHisService;
    private PcVehiclesService pcVehiclesService;

//    @PostMapping("/updateStore")
//    @ApiOperation(value = "编辑店铺")
    public R updateStore(@RequestBody StoreUpdateDTO dto) {
        return R.success(storeService.updateStore(dto));
    }
    @GetMapping("/huixian/{id}")
    @ApiOperation(value = "店铺编辑回显")
    public R huixian(@PathVariable("id") Long id) {
        return R.success(storeService.getById(id));
    }
    @PostMapping("/allStore")
    @ApiOperation(value = "店铺展示")
    public R allStore(@RequestBody AllStoreDTO dto) {
        return R.success(storeService.allStore(dto));
    }
    @GetMapping("/xianqing/{id}")
    @ApiOperation(value = "店铺详情")
    public R xianqing(@PathVariable("id") Long id) {
        return R.success(storeService.xianqing(id));
    }
    @GetMapping("/projectXianqing/{id}")
    @ApiOperation(value = "服务详情", notes = "根据服务id查询")
    public R projectXianqing(@PathVariable("id") Long id) {
        return R.success(storeService.projectXianqing(id));
    }
    @PostMapping("/allStoreScore")
    @ApiOperation(value = "店铺展示评分排序")
    public R allStoreScore(@RequestBody AllStoreDTO dto) {
        return R.success(storeService.allStoreScore(dto));
    }

    @PostMapping("/allStoreScoreHuaWei")
    @ApiOperation(value = "店铺展示评分排序")
    public R allStoreScoreHuaWei(@RequestBody AllStoreDTO dto) {
        return R.success(storeService.allStoreScoreHuaWei(dto));
    }

    @GetMapping("getStore/{id}")
    @ApiOperation(value = "根据id查询店铺", notes = "根据id查询店铺")
    public R getStore(@PathVariable("id") Long id) {
        return R.success(storeService.getById(id));
    }

    @GetMapping("/myScore/{userid}")
    @ApiImplicitParam(name = "userid", value = "用户id", required = true, paramType = "path")
    @ApiOperation(value = "我的店铺   店长 核销人员")
    public R myScore(@PathVariable("userid") Long userid) {
        return storeService.myScore(userid);
    }

    @ApiOperation(value = "查询店铺开关")
    @GetMapping(value = "/getStoreSwitch")
    @LoginMode(strict = false)
    public R<Boolean> getStoreSwitch() {
        Integer storeSwitch = redisRepository.get("storeSwitch");
        if (storeSwitch != null && storeSwitch == 1) {
            return R.success();
        } else {
            return R.success(false);
        }
    }

    @ApiOperation(value = "申请店铺")
    @PostMapping(value = "/applyStore")
    public R applyStore(@RequestBody ApplyStoreDto storeJoin) {
        if (storeJoin.getTelephone().length() != 11){
            return R.fail("手机号格式错误");
        }
        return storeJoinService.applyStore(storeJoin);
    }

    @ApiOperation(value = "申请理疗车")
    @PostMapping(value = "/applyVehicles")
    public R applyVehicles(@RequestBody ApplyVehiclesDto applyVehiclesDto) {
        if (applyVehiclesDto.getPhone().length() != 11){
            return R.fail("手机号格式错误");
        }
        return pcVehiclesService.applyVehicles(applyVehiclesDto);
    }
    @ApiOperation(value = "我的理疗车")
    @GetMapping(value = "/findVehicles/{userid}")
    public R findVehicles(@PathVariable("userid") Long userid) {
        return pcVehiclesService.findVehicles(userid);
    }

    @ApiOperation(value = "修改理疗车信息")
    @PostMapping(value = "/updateVehicles")
    public R updateVehicles(@RequestBody ApplyVehiclesDto applyVehiclesDto) {
        return pcVehiclesService.updateVehicles(applyVehiclesDto);
    }

    @ApiOperation(value = "理疗车服务区域信息查询")
    @GetMapping(value = "/findVehiclesServerRegion")
    public R findVehiclesServerRegion() {
        return pcVehiclesService.findVehiclesServerRegion();
    }

    @ApiOperation(value = "理疗车服务区域信息修改")
    @PostMapping(value = "/updateVehiclesServerRegion")
    public R updateVehiclesServerRegion(@RequestBody VehiclesServerRegionDto regionDto) {
        return pcVehiclesService.updateVehiclesServerRegion(regionDto);
    }

    @ApiOperation(value = "修改店铺信息")
    @PostMapping(value = "/updateStore")
    public R updateStore(@RequestBody ApplyStoreDto storeJoin) {
        return storeJoinService.updateStore(storeJoin);
    }
    
    
    @ApiOperation(value = "添加核销人员")
    @PostMapping(value = "/addverify")
    public R addverify(@RequestBody AddverifyDto dto) {
        //短信验证码校验
        String key = CacheKey
                .buildKey(CacheKey.ADD_verify, TemplateCodeType.CODE_Add_verify.name(),
                        dto.getPhone());
        String code = cacheRepository.get(key);
        if (StrUtil.isBlank(code) || !code.equals(dto.getCode())) {
            return R.fail(code, "验证码错误");
        }
        return storeService.addverify(dto);
    }

    @ApiOperation(value = "提现")
    @PostMapping(value = "/appletWithdraw")
    public R appletWithdraw(@RequestBody AppletWithdraw appletWithdraw) {
        return appletStoreServerService.appletWithdraw(appletWithdraw);
    }

    @ApiOperation(value = "查询提现历史", notes = "查询提现历史")
    @PostMapping(value = "/findWithdrawHis")
    public R<IPage<AppletWithdraw>> findWithdrawHis(@RequestBody PageParams<AppletWithdraw> dto) {
        AppletWithdraw model = dto.getModel();
        Long mid = BaseContextHandler.getUserId();
        model.setMid(mid);
        IPage page = dto.getPage();
        return storeService.findWithdrawHis(page, model);
    }

    @ApiOperation(value = "店铺核销人员")
    @ApiImplicitParam(name = "storeId", value = "店铺id", required = true, paramType = "path")
    @GetMapping(value = "/verify/{storeId}")
    public R verify(@PathVariable("storeId") Long storeId) {
        return storeService.verify(storeId);
    }

    @ApiOperation(value = "删除核销人员")
    @GetMapping(value = "/deleteVerify/{mid}")
    public R deleteVerify(@PathVariable("mid") Long mid) {
        return storeService.deleteVerify(mid);
    }


    @ApiOperation(value = "店铺添加服务")
    @PostMapping(value = "/addstoreProject")
    public R addstoreProject( @RequestBody Map<String,Object> vo) {
      return storeService.addstoreProject(vo);
    }

    @ApiOperation(value = "店铺删除服务")
    @ApiImplicitParam(name = "id", value = "店铺服务id", required = true, paramType = "path")
    @GetMapping(value = "/delectstoreProject/{id}")
    public R delectstoreProject(@PathVariable("id") Long id) {
        return storeService.delectstoreProject(id);
    }

    @ApiOperation(value = "店铺服务的服务")
    @ApiImplicitParam(name = "storeid", value = "店铺id", required = true, paramType = "path")
    @GetMapping(value = "/storeProject/{storeid}")
    public R storeProject(@PathVariable("storeid") Long storeid) {
        return storeService.storeProject(storeid);
    }


//    @ApiOperation(value = "查询提现历史", notes = "查询提现历史")
//    @PostMapping(value = "/findXsyWithdrawHis")
//    public R<IPage<StoreWithdrawHis>> findXsyWithdrawHis(@RequestBody PageParams<StoreWithdrawHisPageDTO> dto) {
//        StoreWithdrawHisPageDTO model = dto.getModel();
//        Long mid = BaseContextHandler.getUserId();
//        model.setMid(mid);
//        IPage page = dto.getPage();
//        return storeWithdrawHisService.findXsyWithdrawHis(page, model);
//    }


    @ApiOperation(value = "今日 预约、营业额", notes = "今日 预约.营业额")
    @GetMapping(value = "/today/{storeid}")
    @ApiImplicitParam(name = "storeid", value = "店铺id", required = true, paramType = "path")
    public R today(@PathVariable("storeid") Long storeid) {
        return storeWithdrawHisService.today(storeid);
    }
    @ApiOperation(value = "获取所有的市")
    @GetMapping("/getcity")
    public List<String> getcity(){
        return storeService.getcity();
    }
    @ApiOperation(value = "判断店铺是否存在")
    @GetMapping("/selectstoreid/{id}")
    public R selectstoreid(@PathVariable("id") Long id){
        return storeService.selectstoreid(id);
    }
}
